//
//  SearchViewController.m
//  floodAdvisor
//
//  Created by Long Ho on 2/22/10.
//  Copyright 2010 Lafayette College. All rights reserved.
//

#import "SearchViewController.h"



@implementation SearchViewController

@synthesize streetTextField;
@synthesize stateTextField;
@synthesize cityTextField;
@synthesize zipcodeTextField;
@synthesize searchScrollView;
@synthesize searchButton;


static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;


- (IBAction) search: (id) sender {
	NSLog(@"%@",[streetTextField text]);
	NSLog(@"%@",[stateTextField text]);
	NSLog(@"%@",[cityTextField text]);
	NSLog(@"%@",[zipcodeTextField text]);
	//[searchButton setTitle: @"Something" forState:UIControlStateNormal];
}


//Credits: http://cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html
- (void) textFieldDidBeginEditing: (UITextField*) textField {
	CGRect textFieldRect =
	[self.searchScrollView.window convertRect:textField.bounds fromView:textField];
    CGRect viewRect =
	[self.searchScrollView.window convertRect:searchScrollView.bounds fromView:self.searchScrollView];
	
	CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
    CGFloat numerator =
	midline - viewRect.origin.y
	- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
    CGFloat denominator =
	(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
	* viewRect.size.height;
    CGFloat heightFraction = numerator / denominator;
	
	if (heightFraction < 0.0)
    {
        heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
        heightFraction = 1.0;
    }
	
	UIInterfaceOrientation orientation =
	[[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
    }
    else
    {
        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
    }
	
	CGRect viewFrame = self.searchScrollView.frame;
    viewFrame.origin.y -= animatedDistance;
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
    
    [self.searchScrollView setFrame:viewFrame];
    
    [UIView commitAnimations];
}


//Credits: http://cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html
- (void) textFieldDidEndEditing: (UITextField *)textField { //keyboard will hide
	CGRect viewFrame = self.searchScrollView.frame;
    viewFrame.origin.y += animatedDistance;
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
    
    [self.searchScrollView setFrame:viewFrame];
    
    [UIView commitAnimations];
}

#pragma mark UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	
	[textField resignFirstResponder];
	
	return YES;
	
}

- (IBAction) makeKeyboardGoAway{
	[streetTextField resignFirstResponder];
	[stateTextField resignFirstResponder];
	[cityTextField resignFirstResponder];
	[zipcodeTextField resignFirstResponder];
}


/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[streetTextField release];
	[cityTextField release];
	[stateTextField release];
	[zipcodeTextField release];
	[searchScrollView release];
    [super dealloc];
}


@end
